package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.controller.base.BaseController;
import com.lbssoft.gpsbd.dao.DeviceGeofenceMapper;
import com.lbssoft.gpsbd.dao.DeviceMapper;
import com.lbssoft.gpsbd.model.Device;
import com.lbssoft.gpsbd.model.GroupDeviceTree;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.DeviceGeofencesParam;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api("设备管理")
@RestController
@RequestMapping(value="/devices")
public class DeviceController extends BaseController{
	@Autowired
	DeviceMapper deviceMapper;

	@Autowired
	DeviceService deviceService;
	
	@Autowired
	DeviceGeofenceMapper deviceGeofenceMapper;


	//@Transactional
	@ApiOperation(value="添加设备", notes="添加设备到业务系统")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="body",name = "device", value = "添加设备", required = true, dataType = "Device"),
	})
	@RequiresPermissions("user:device:add")
	@LogAnnotation(targetType = "device",action = "add",remark = "添加设备")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody Device device) {
		return  deviceService.add(device);
	}
	

	@ApiOperation(value="更新设备信息", notes="更新设备信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="path",name = "id", value = "设备id", required = true, dataType = "int"),
		@ApiImplicitParam(paramType="body",name = "device", value = "设备信息", required = true, dataType = "Device"),
	})
	@RequiresPermissions("user:device:update")
	@LogAnnotation(targetType = "device",action = "update",remark = "更新设备信息")
	@RequestMapping(value = "/{id}",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@NotNull @PathVariable int id,@Validated @RequestBody Device device) {
		device.setId(id);
		return  deviceService.update(device);
	}
	
	@ApiOperation(value="删除设备", notes="删除设备，需要同步删除采集中心设备")
	@ApiImplicitParam(paramType="path",name = "id", value = "设备id", required = true, dataType = "long")
	@RequiresPermissions("user:device:delete")
	@LogAnnotation(targetType = "device",action = "delete",remark = "删除设备")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@PathVariable long id) {
		return  deviceService.delete(id);
	}
	
	@ApiOperation(value="用户获取设备列表", notes="用户获取所有设备状态")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list() {
		return deviceService.list();
	}

	@ApiOperation(value="管理员获取所有设备列表", notes="管理员获取所有设备列表")
	@RequiresPermissions("system:device:view")
	@RequestMapping(value = "/all",method = {RequestMethod.GET})
	@ResponseBody
	public Result all(@NotEmpty @RequestParam(value = "page", defaultValue = "1") int page,
					  @NotEmpty @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
					  @RequestParam(value = "column", defaultValue = "") String column,
					  @RequestParam(value = "keyword", defaultValue = "") String keyword) {
		return deviceService.getDevices(page,pageSize,column,keyword);
	}
	
	
	@ApiOperation(value="获取绑定的围栏", notes="根据设备id获取绑定的围栏ids")
	@RequestMapping(value = "/geofenceids/{id}",method = {RequestMethod.GET})
	@ResponseBody
	public Result getGeofenceIds(@PathVariable long id) {
		Result result = new Result();
		List<Integer> deviceIds = deviceGeofenceMapper.getGeofenceIds(id);
		result.setCode(0);
		result.setData(deviceIds);
		return result;
	}
	
	@ApiOperation(value="设备绑定围栏", notes="根据设备绑定围栏")
	@RequiresPermissions("user:geofence:binddevice")
	@LogAnnotation(targetType = "device",action = "update",remark = "设备绑定围栏")
	@RequestMapping(value = "bind",method = {RequestMethod.POST})
	@ResponseBody
	public Result bindGeofences(@RequestBody DeviceGeofencesParam param) {
		Result result = new Result();
		//todo 需要同步到采集中心
		for(Integer geofenceId:param.getGeofenceIds()){
			deviceGeofenceMapper.addDeviceGeofence(param.getDeviceId(), geofenceId);
		}
		result.setCode(0);
		return result;
	}
	
	@ApiOperation(value="设备解除绑定围栏", notes="根据设备解除绑定的围栏")
	@RequiresPermissions("user:geofence:binddevice")
	@LogAnnotation(targetType = "device",action = "update",remark = "设备解除绑定围栏")
	@RequestMapping(value = "unbind",method = {RequestMethod.POST})
	@ResponseBody
	public Result unbindGeofences(@RequestBody DeviceGeofencesParam param) {
		Result result = new Result();
		//todo 需要同步到采集中心
		for(Integer geofenceId:param.getGeofenceIds()){
			deviceGeofenceMapper.delete(param.getDeviceId(), geofenceId);
		}
		result.setCode(0);
		return result;
	}

	@ApiOperation(value="获取分组设备的树形结构", notes="获取分组设备的树形结构")
	@RequestMapping(value = "tree",method = {RequestMethod.GET})
	@ResponseBody
	public Result tree() {
		UserVo user = (UserVo)SecurityUtils.getSubject().getPrincipal();
		Result result = new Result();
		List<GroupDeviceTree> list = deviceService.getTree(user.getId());
		result.setData(list);
		return result;
	}

	@ApiOperation(value="获取设备的信息", notes="根据设备id获取设备信息")
	@RequestMapping(value = "/{id}",method = {RequestMethod.GET})
	@ResponseBody
	public Result info(@PathVariable long id) {
		UserVo user = (UserVo)SecurityUtils.getSubject().getPrincipal();
		Result result = new Result();
		result.setData(deviceService.getInfo(id));
		return result;
	}
	
}